package test;

import com.sun.org.apache.xerces.internal.dom.AbortException;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.SneakyThrows;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;


import java.util.concurrent.*;

public class Test202104041 {

    public static void main(String[] args) throws InterruptedException {
        ThreadFactory threadFactory = new CustomizableThreadFactory("springThread-pool-");
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4,
                16,
                100,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(3),
                threadFactory,
                new java.util.concurrent.ThreadPoolExecutor.AbortPolicy());
        final CountDownLatch cdOrder = new CountDownLatch(1);
        final CountDownLatch cdResult = new CountDownLatch(4);

        for (int i = 0; i < 4; i++) {
            Runnable runnable = new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    System.out.println("选手" + Thread.currentThread().getName() + "正在等待裁判发布口令");
                    cdOrder.await();
                    System.out.println("选手" + Thread.currentThread().getName() + "已接受裁判口令");
                    double time = Math.random()*20;
                    System.out.println("选手"+Thread.currentThread().getName()+"消耗的时间为"+time+"s");
                    Thread.sleep((long) (time));
                    System.out.println("选手" + Thread.currentThread().getName() + "到达终点");
                    cdResult.countDown();
                }
            };
            threadPoolExecutor.execute(runnable);
        }
        Thread.sleep((long) (Math.random() * 3_000));
        cdOrder.countDown();
        cdResult.await();
        threadPoolExecutor.shutdown();


    }

}
